package com.cgwx.controller;

import com.cgwx.Utils.result.ResultUtil;
import com.cgwx.config.BusinessException;
import com.cgwx.entity.*;
import com.cgwx.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Api(tags = "个人空间")
@ApiIgnore
@RequestMapping("/personal")
public class TestPersonalController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IFolderService folderService;

    @Resource
    private ISharedService sharedService;

    @Resource
    private IFavoritesService favoritesService;

    @Resource
    private IRoomService roomService;

    @Resource
    private IPersonalRecycleBinService personalRecycleBinService;

    @GetMapping("/hello")
    @ApiOperation("测试打通")
    public Object hello() {
        return ResultUtil.success("hello");
    }

    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    public Object uploadFile(@RequestParam String account, @RequestParam String name, @RequestParam String deptId, @RequestParam String parentId, String type, @RequestParam MultipartFile multipartFile) {
        Folder folder = new Folder();
        folder.setEntityId(parentId);
        String roomId = folderService.getFolder(folder).get(0).getEntityRoom();
        String result = utilService.uploadFile(account, name, deptId, roomId, parentId, type, multipartFile, "false");
        return ResultUtil.success(result);
    }

    @PostMapping("/downloadFiles")
    @ApiOperation("下载文件")
    public Object downloadFiles(@RequestParam String entityId) {
        String account = "0064";
        String deptId = "000306";
        String type = "dept";
        String Url = utilService.getURLByFileId(account, deptId, type, entityId);
        return ResultUtil.success(Url);
    }

    @PostMapping("/getPersonalTree")
    @ApiOperation("获取个人文件树")
    public Object getPersonalTree(@RequestBody UsUser user) {
        List<Folder> result = folderService.getPersonalTree(user.getAccount());
        return ResultUtil.success(result);
    }

    @PostMapping("/insertPersonalFolder")
    @ApiOperation("新增个人文件夹")
    public Object insertPersonalFolder(@RequestBody UsUser user, @RequestParam String entityName, @RequestParam String parentId) {
        String account = user.getAccount();
        String userName = user.getName();
        String result = folderService.insertPersonalFolder(account, userName, entityName, parentId);
        List<Folder> folders = folderService.getPersonalTree(account);
        return ResultUtil.success(folders, result);
    }

    @PostMapping("/rename")
    @ApiOperation("重命名文件(夹)")
    public Object rename(@RequestBody UsUser user, @RequestParam String entityName, @RequestParam String entityId, @RequestParam String parentId, @RequestParam String type) {
        String result = utilService.rename(user.getAccount(), user.getDeptId(), entityName, entityId, parentId, type);
        return ResultUtil.success(result);
    }

    @PostMapping("/deletePersonal")
    @Transactional
    @ApiOperation("根据entityId删除个人文件（夹）")
    public Object deletePersonal(@RequestParam String account, @RequestParam String deptId, @RequestParam String[] entityIds, @RequestParam String type) {
        String result = utilService.delete(account, deptId, Arrays.asList(entityIds), type);
        return ResultUtil.success(result);
    }

    @PostMapping("/restorePersonal")
    @Transactional
    @ApiOperation("根据entityId恢复个人文件（夹）")
    public Object restorePersonal(@RequestParam String account, @RequestParam String deptId, @RequestParam String[] entityIds, @RequestParam String type) {
        String result = utilService.restore(account, deptId, Arrays.asList(entityIds), type);
        return ResultUtil.success(result);
    }

    @PostMapping("/remove")
    @Transactional
    @ApiOperation("根据parentId移动个人文件（夹）")
    public Object remove(@RequestBody UsUser user, @RequestParam String entityId, @RequestParam String parentId, @RequestParam String type) {
        String result = utilService.remove(user.getAccount(), user.getDeptId(), entityId, parentId, type);
        return ResultUtil.success(result);
    }

    @PostMapping("/removePersonalRecycleBin")
    @Transactional
    @ApiOperation("从回收站移动到个人空间")
    public Object removePersonalRecycleBin(@RequestBody UsUser user, @RequestParam String entityId, @RequestParam String parentId, @RequestParam String type) {
        String result = utilService.removeRecycleBin(user.getAccount(), user.getDeptId(), entityId, parentId, type);
        return ResultUtil.success(result);
    }

    @PostMapping("/deletePersonalEntirely")
    @ApiOperation("彻底删除")
    public Object deletePersonalEntirely(@RequestParam String[] entityIds) {
        String account  = "0772";
        String result = personalRecycleBinService.deleteRecycleBinEntirelyById(Arrays.asList(entityIds));
        List<Folder> folders = folderService.getPersonalTree(account);
        return ResultUtil.success(folders, result);
    }

    @GetMapping("/getUsedSpace")
    @ApiOperation("获取空间大小")
    public Object getUsedSpace(@RequestBody UsUser user, String type) {
        String result = roomService.getUsedSpace(user.getAccount(), type);
        return ResultUtil.success(result);
    }

    @PostMapping("/insertShared")
    @ApiOperation("分享个人文件（夹）")
    @Transactional
    public Object insertShared(@RequestParam String[] entityIds, @RequestParam String[] sharedIds) {
        Shared shared = new Shared();
        shared.setAccount("0772");
        List<String> sharedList = new ArrayList<>(new LinkedHashSet<>(Arrays.asList(sharedIds)));
        String result;
        for(String sharedId: sharedList){
            shared.setSharedId(sharedId);
            for(String entityId: entityIds){
                shared.setEntityId(entityId);
                //分享去重
                List<Shared> shareds = sharedService.getShared(shared);
                if(shareds.size() > 0){
                    Shared shared1 = shareds.get(0);
                    boolean flag = false;
                    if(shared1.getIsCanceled().equals("true")){
                        shared1.setIsCanceled("false");
                        flag = true;
                        if(sharedService.updateSharedById(shared1).equals("更新失败!")) throw new BusinessException("分享更新失败!");
                    }
                    if(shared1.getDeleteTime() != null){
                        shared1.setDeleteTime(null);
                        flag = true;
                        if(sharedService.updateSharedById(shared1).equals("更新失败!")) throw new BusinessException("分享更新失败!");
                    }
                    if(!flag) throw new BusinessException("已存在该分享!");
                    result = "分享成功!";
                }
                else result = sharedService.insertShared(shared);
                if(!result.equals("分享成功!")) throw new BusinessException("分享失败!");
            }
        }
        return ResultUtil.success("分享成功!");
    }

    @PostMapping("/deleteShared")
    @ApiOperation("被分享人根据分享id删除分享记录")
    public Object deleteShared(@RequestParam String[] ids) {
        String result = sharedService.deleteSharedById(Arrays.asList(ids));
        return ResultUtil.success(result);
    }

    @PostMapping("/cancelShared")
    @ApiOperation("分享人根据分享id取消分享")
    public Object cancelShared(@RequestBody UsUser user, @RequestParam String[] ids) {
        String result = sharedService.cancelSharedById(Arrays.asList(ids));
        return ResultUtil.success(result);
    }

    @PostMapping("/restoreShared")
    @ApiOperation("被分享人根据分享id恢复分享记录")
    public Object restoreShared(@RequestParam String[] ids) {
        String result = sharedService.restoreSharedById(Arrays.asList(ids));
        return ResultUtil.success(result);
    }

    @PostMapping("/insertFavorites")
    @ApiOperation("收藏")
    public Object insertFavorites(@RequestParam String[] entityIds) {
        String account = "0064";
        String result = favoritesService.insertFavorites(account, Arrays.asList(entityIds));
        return ResultUtil.success(result);
    }

    @PostMapping("/deleteFavorites")
    @ApiOperation("取消收藏")
    public Object deleteFavorites(@RequestBody UsUser user, @RequestParam String[] entityIds) {
        String result = favoritesService.deleteFavorites(user.getAccount(), Arrays.asList(entityIds));
        return ResultUtil.success(result);
    }
}
